package com.ace3l.legal.gwt.domainmodule.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.ace3l.legal.gwt.domainmodule.client.ProjectService;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTProject;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTUser;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTLawFirm;
import com.ace3l.legal.gwt.domainmodule.gwt.GWTRole;
import com.ace3l.legal.gwt.domainmodule.domainxform.ProjectXForm;
import com.ace3l.legal.backend.dao.UserDao;
import com.ace3l.legal.backend.dao.LawFirmDao;
import com.ace3l.legal.backend.dao.ProjectDao;
import com.ace3l.legal.backend.domain.LawFirm;
import com.ace3l.legal.backend.domain.Project;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.util.List;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 23, 2008
 * Time: 1:29:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProjectServiceImpl extends RemoteServiceServlet implements ProjectService {
    public GWTProject createProject(GWTProject p, Integer myId) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

        LawFirmDao lawDao = (LawFirmDao)appCtx.getBean("lawFirmDao");
        ProjectDao projectDao = (ProjectDao)appCtx.getBean("projectDao");

        Project myProject = ProjectXForm.fromGWT(p);
        LawFirm myLawFirm = lawDao.getLawFirm(myId);

        myProject.setLawFirm(myLawFirm);
        myProject.setHoursCompleted(0);
        myProject.setWorkflowStage("NEW");
        return ProjectXForm.toGWT(projectDao.saveProject(myProject));

    }

    public String[][] getProjectsForLawFirm(Integer lawFirmId) {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ProjectDao projectDao = (ProjectDao)appCtx.getBean("projectDao");
        List<Project> projects = projectDao.getProjectsForLawFirm(lawFirmId);
        
        String[][] out = new String[projects.size()][5];
        for (int i = 0; i < projects.size(); i++) {
            Project me = projects.get(i);
            out[i] = new String[] {me.getTitle(),
                    me.getDueDate().toString(),
                    me.getHoursAllotted().toString(),
                    me.getHoursCompleted().toString(),
                    me.getWorkflowStage()};
        }
        return out;
    }

}